package com.guodu.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialAppraisalDao;
import com.guodu.entity.MaterialAppraisal;
import com.guodu.entity.Units;
import com.guodu.vo.MaterialAppraisalQueryVo;


@Repository("materialAppraisalDao")
public class MaterialAppraisalDaoImpl extends BaseDao implements MaterialAppraisalDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialAppraisal> findByVo(MaterialAppraisalQueryVo maqv,
			Units units) {
		// TODO Auto-generated method stub\
		StringBuffer hql = new StringBuffer("from MaterialAppraisal m where 1=1");
		if(maqv!=null){
			if (maqv.getMonths()!=null && !"".equals(maqv.getMonths())) {
				hql.append("    and TO_DATE(months,'yyyy-MM') = TO_DATE('"+maqv.getMonths()+"','yyyy-MM')");
			}
		}
		hql.append(" order by zf desc");
		return super.find(hql.toString());
	}

	@Override
	public MaterialAppraisal findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (MaterialAppraisal) super.findById(MaterialAppraisal.class, purchaseId);
	}

	@Override
	public void saveorUpdate(MaterialAppraisal materialAppraisal) {
		// TODO Auto-generated method stub
		String hql = "delete from MaterialAppraisal where unitsId="+materialAppraisal.getUnitsId()+" and months='"+materialAppraisal.getMonths()+"'";
		super.executeUpdate(hql);
		super.saveOrUpdate(materialAppraisal);
	}

	@Override
	public List<Object[]> findLjCount(MaterialAppraisalQueryVo maqv) {
		// TODO Auto-generated method stub
		String hql = "select unitsId, round(avg(zf),2) from MaterialAppraisal where months>='"+maqv.getMonths().split("-")[0]+"-01' and months <='"+maqv.getMonths()+"' group by unitsId order by sum(zf) desc ";
		return super.find(hql);
	}

}
